package com.bdtop.dbl.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.bdtop.dbl.R;
import com.bdtop.dbl.utils.ImageUtils;

public class RadarScanView extends View {

	public RadarScanView(Context context) {
		super(context);
		init();
	}

	public RadarScanView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public RadarScanView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mScanningBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.radar_body_sca);
		// mScanningBitmap = ImageUtils.reverseImage(mScanningBitmap, 1);

		mScanningBitmap = ImageUtils.rotateImage(mScanningBitmap, 180);
	}

	public void startScanning() {
		if (!isScanning) {
			isScanning = true;
			if (mHandler == null) {
				mHandler = new Handler();
			}
			mHandler.post(mRefreshRunnable);
		}
	}

	// fix
	public void startScanning(boolean resetScanning) {
		isScanning = !resetScanning;
		if (!isScanning) {
			isScanning = true;
			if (mHandler == null) {
				mHandler = new Handler();
			}
			mHandler.post(mRefreshRunnable);
		}
	}

	public void stopScanning() {
		isScanning = false;
		if (mHandler != null) {
			mHandler.removeCallbacks(mRefreshRunnable);
			mHandler = null;
		}

		invalidate();
	}

	public boolean isScanning() {
		return isScanning;
	}

	private Runnable mRefreshRunnable = new Runnable() {
		@Override public void run() {
			mAngleOffset += 8;
			invalidate();

			if (mHandler != null) {
				mHandler.postDelayed(this, TIME_POST_DEALAY);
			}

		}
	};

	private final int TIME_POST_DEALAY = 15;
	private float mAngleOffset = 0;
	private boolean isScanning = false;

	private int mCenterX;
	private int mCenterY;

	private Bitmap mScanningBitmap;
	private Handler mHandler;

	private Paint paint = new Paint();

	@SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		mCenterX = (int) (getWidth() * 0.5);
		mCenterY = (int) (getHeight() * 0.9);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);

		if (isScanning) {

			if (mAngleOffset == 0) {
				// canvas.drawBitmap(mScanningBitmap, mCenterX -
				// mScanningBitmap.getWidth(), mCenterY,
				// null);
			} else {
				if(mScanningBitmap != null ){
					Rect rMoon = new Rect(mCenterX - mScanningBitmap.getWidth() / 2, mCenterY, mCenterX + mScanningBitmap.getWidth() / 2,
							mCenterY + mScanningBitmap.getHeight());
	
					canvas.rotate(mAngleOffset, mCenterX, mCenterY);
					canvas.drawBitmap(mScanningBitmap, null, rMoon, null);
				}
			}

		}

	}

}
